<?php
class ProductsController extends AppController {

	var $name = 'Products';
				
	var $helpers = array('Html', 'Session','Javascript','Link');
				
	var $uses = array('User');
	
		function beforeFilter(){
		$this->Auth->allow(array('index','product', 'cart', 'DeptProducts'));
		parent::beforeFilter();
    }
	
	
	function index($category_slug="", $parent="")
		{
			//echo "index";
			$this->layout ="default";
			
			if(isset($category_slug) and ($category_slug) and ($category_slug!="cart"))
				{
					$cat_id  = $this->__get_slug_to__id($category_slug, "Category");
					$departments =  $this->Department->find('all', array('conditions'=>'Department.cat_id Like "%'.$cat_id.'%"')); 
					$this->Set('departments',$departments); 
					if(isset($cat_id) and ($cat_id) and sizeof($cat_id)):	
				     	$cats = $this->__get_details( $cat_id, "Category");
					if(isset($cats) and ($cats)):
					  $this->Set('Category', $cats);
					endif;
								if(isset($parent) and (base64_decode($parent)=="Parent") and ($parent))
									{   
									//echo $cat_id;
									//..exit;
									   $total_products = $this->Product->find('count',array('conditions' => 'Product.parent_cat_id="'.$cat_id.'"') );
									   
									   
									   $this->paginate['Product'] =array('conditions' => 'Product.parent_cat_id="'.$cat_id.'"',
												'limit' => 24,
												'fields' => array('Product.*') ,
												'order' => 'Product.created Desc'
											  );
									}
								else
									{
										$total_products = $this->Product->find('count',array('conditions' => 'Product.cat_id="'.$cat_id.'"') );
										
										
										$this->paginate['Product'] =array('conditions' => 'Product.cat_id="'.$cat_id.'"',
														'limit' => 24,
														'fields' => array('Product.*') ,
														'order' => 'Product.created Desc'
													  );
									}
								$this->Set('total_products', $total_products); 
							    $products = $this->paginate('Product');
								
							if(isset($products) and ($products))
								{   $x =0;
									foreach($products as $product)
										{
											$stock_detail = $this->_get_stock_position($product['Product']['stock']);
											$products[$x]['Product']['stock_detail']= $stock_detail; 
											$MasterProduct = $this->__get_product_min_price($product['Product']['id']);
											if(isset($MasterProduct) and ($MasterProduct))
												{
													$products[$x]['Product']['MasterProduct'] = $MasterProduct;
												}
											else
												{
													unset($products[$x]);
												}
											$x++;
										}
										//pr( $products );
										//exit;
								   $this->Set('products', $products);
								}
					endif;
				}
			/* End Categories Section */
		}
		function DeptProducts($department_slug="")
		{
			$this->layout ="default";
			if(isset($department_slug) and ($department_slug))
				{
					$dept_id  = $this->__get_slug_to__id($department_slug, "Department");
					
					if(isset($dept_id) and ($dept_id) and sizeof($dept_id)):	
					$dept = $this->__get_details( $dept_id, "Department");
					if(isset($dept) and ($dept)):
					  $this->Set('dept', $dept);
					endif;
					$this->paginate['Product'] =array('conditions' => 'Product.dept_id="'.$dept_id.'"',
									'limit' => 24,
									'fields' => array('Product.*') ,
									'order' => 'Product.created Desc'
								  );
					$products = $this->paginate('Product');
					if(isset($products) and ($products))
						{   $x =0;
							foreach($products as $product)
								{
									$stock_detail = $this->_get_stock_position($product['Product']['stock']);
									$products[$x]['Product']['stock_detail']= $stock_detail; 
									$MasterProduct = $this->__get_product_min_price($product['Product']['id']);
									if(isset($MasterProduct) and ($MasterProduct))
										{
											$products[$x]['Product']['MasterProduct'] = $MasterProduct;
										}
									else
										{
											unset($products[$x]);
										}
									
									$x++;
								}
						   $this->Set('products', $products);
						}
					endif;
				}
		}

	function product($product_slug="")
		{
			$product_details = $this->Product->findBySlug($product_slug);
			$category = $this->Category->findById($product_details['Product']['cat_id']);
			$product_details['Product']['category']= $category['Category']; 
			$stock_detail = $this->_get_stock_position($product_details['Product']['stock']);
			$product_details['Product']['stock_detail']= $stock_detail; 
			$MasterProduct = $this->__get_product_min_price($product_details['Product']['id']);
			$product_details['Product']['MasterProduct']= $MasterProduct;

			$this->Set('product_detail', $product_details); 
			
		}
	
	function cart($action="",$product_slug="")
		{
			   switch($action)
				 {
						case "add":
							if(isset($product_slug) and ($product_slug))
							{
								$exp_product_slug = explode("^", $product_slug);
								if(isset($exp_product_slug[0]) and ($exp_product_slug[0]))
									{
										$product_slug = $exp_product_slug[0];
										if(isset($exp_product_slug[1]) and ($exp_product_slug[1])){
										$up_quantity_1 = $exp_product_slug[1];
										}else{
											$up_quantity_1 = 1; 
										}
									}
									else
									{
										$product_slug = $product_slug;
									}
									$product_id = $this->__get_slug_to__id($product_slug,"Product");
									$current_cart = $this->Session->read('cart');
									if(isset($current_cart['product_id']) and ($current_cart['product_id']))
										{
												  $already_exist = array_key_exists($product_id, $current_cart['product_id']);
												  if(isset($already_exist) and ($already_exist))
														{
															//echo "Item Already in Cart! check Your Cart<br>";
														if(isset($up_quantity_1) and ($up_quantity_1))
															{
																$new_quantity = $up_quantity_1; 
															}
														else
															{
																$new_quantity = $current_cart['product_id'][$product_id]['Cart_quantity']+1;
															}
															$new_price =
															$new_quantity * $current_cart['product_id'][$product_id]['product_price'] ;
															$current_cart['product_id'][$product_id]['id'] = $current_cart['product_id'][$product_id]['id']; 
															$current_cart['product_id'][$product_id]['Cart_price'] =$new_price; 
															$current_cart['product_id'][$product_id]['Cart_quantity'] = $new_quantity; 
															$current_cart['product_id'][$product_id]['product_price']  = 
															$current_cart['product_id'][$product_id]['product_price'] ;
															$new_price_repeat = $current_cart['total_price']
																		+$current_cart['product_id'][$product_id]['product_price'];
															$this->Session->write('cart.product_id.'.$product_id,$current_cart['product_id'][$product_id]);
															$this->Session->write('cart.total_price',$new_price_repeat);
														}
													else
														{
							$product_details = $this->Product->findById($product_id, 
																  array('fields'=>'Product.*
												'));
							pr($product_details);
							exit;					
							$stock_detail = $this->_get_stock_position($product_details['Product']['stock']);
							$product_details['Product']['stock_detail']= $stock_detail;
							if(isset($product_details['Product']['stock']) and ($product_details['Product']['stock']=="2"))
								{
									if(isset($product_details['Product']['qty']) and ($product_details['Product']['qty']>0))
									{   
										$ProductCart['ProductCart']['id'] = $product_details['Product']['id'];
										$ProductCart['ProductCart']['Cart_quantity'] = 1; // It will come dynamicaly 
										$ProductCart['ProductCart']['Cart_price'] =
										($ProductCart['ProductCart']['Cart_quantity']*$product_details['Product']['price']);
										$ProductCart['ProductCart']['product_price'] = $product_details['Product']['price'];
										$current_count = count($current_cart['product_id']);
										$current_cart['product_id'][$ProductCart['ProductCart']['id']] = $ProductCart['ProductCart'];
										$new_price = $current_cart['total_price']
										+$ProductCart['ProductCart']['product_price'];
										$this->Session->write('cart.product_id',$current_cart['product_id']);
										$this->Session->write('cart.total_price',$new_price);
									}
								}
														}
										}
										else
										{
										//echo "else";
										$product_details = $this->Product->findById($product_id, 
																  array('fields'=>'Product.*
										'));
										
										$MasterProduct = $this->__get_product_min_price($product_details['Product']['id']);
										
										$product_details['Product']['price'] = $MasterProduct['price'];
										$product_details['Product']['seller_id'] = $MasterProduct['seller_id'];
										$stock_detail = $this->_get_stock_position($product_details['Product']['stock']);
										$product_details['Product']['stock_detail']= $stock_detail;
										if(isset($product_details['Product']['stock']) and ($product_details['Product']['stock']=="2"))
											{
											if(isset($product_details['Product']['qty']) and ($product_details['Product']['qty']>0))
												{      
													$ProductCart['ProductCart']['id'] = $product_details['Product']['id']; 
													$ProductCart['ProductCart']['Cart_quantity'] = 1; // It will come dynamicaly 
													$ProductCart['ProductCart']['Cart_price'] =
													($ProductCart['ProductCart']['Cart_quantity']*$product_details['Product']['price']);
													$ProductCart['ProductCart']['product_price'] = $product_details['Product']['price'];
													$current_cart['product_id'][$ProductCart['ProductCart']['id']] = $ProductCart['ProductCart'];
													$this->Session->write('cart.product_id',$current_cart['product_id']);
													$this->Session->write('cart.total_price',$ProductCart['ProductCart']['Cart_price']);
												}
											}
										}
										$current_cart = $this->Session->read('cart');
										if(isset($current_cart) and sizeof($current_cart))
											{
												$total_sum = 0;
												foreach($current_cart['product_id'] as $cart)
													{
														$total_sum = $total_sum + $cart['Cart_price'];
													}
												$this->Session->write('cart.total_price',$total_sum);
												$current_cart_final = $this->Session->read('cart');
												$x=0;
										foreach($current_cart_final['product_id'] as $cart)
											{
												$product_details = $this->Product->findById($cart['id']);
												$stock_detail = $this->_get_stock_position($product_details['Product']['stock']);
												$product_details['Product']['stock_detail']= $stock_detail; 
												$current_cart_final['product_id'][$cart['id']]['Product'] = $product_details['Product']; 
												$this->Session->write('cart.product_id.'.$cart['id'].'.Product',
												$current_cart_final['product_id'][$cart['id']]['Product']);
												$x++;
											}
										$this->redirect(array('controller'=>'products', 'action'=>'cart', 'view'));
									}
							}
							break;
						case "delete":
							$current_cart = $this->Session->read('cart');
							$product_id = $this->__get_slug_to__id($product_slug,"Product");
							if(isset($product_id) and ($product_id))
								{
									if(isset($current_cart['product_id'][$product_id]) and ($current_cart['product_id'][$product_id]))
										{
											$item_price = $current_cart['product_id'][$product_id]['Cart_price'];
											$total_cart_price = $current_cart['total_price']; 
											$now_total = $total_cart_price-$item_price;
											$this->Session->delete('cart.product_id.'.$product_id);
											$this->Session->write('cart.total_price',$now_total); 
											$this->redirect(array('controller'=>'products', 'action'=>'cart', 'view'));
										}
								}
								else
								{
									$this->redirect(array('controller'=>'products', 'action'=>'cart', 'view'));
								}
								
						break;
						default:
						   $current_cart = $this->Session->read('cart');
						   $this->Set('current_cart', $current_cart);
						break;
				 }
			
		}
		
}
